/*
 * Copyright 2012 Faisal Aslam.
 * All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * only, as published by the Free Software Foundation.
 * 
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 3 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License
 * version 3 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Faisal Aslam (faisal.aslam AT gmail.com)
 * if you need additional information or have any questions.
 */
package takatuka.classreader.logic.parallel.addAttr;

import takatuka.classreader.dataObjs.*;
import takatuka.classreader.dataObjs.attribute.AttributeInfoController;
import takatuka.classreader.dataObjs.attribute.GenericAtt;
import takatuka.classreader.dataObjs.constantPool.*;
import takatuka.classreader.logic.factory.*;
import takatuka.classreader.logic.parallel.readFile.*;

/**
 *
 *
 * @author Faisal Aslam
 */
public class AddAttr {

    private static final String FILE_NAME = "parallelInput";
    private static final String MAIN_CLASS_NAME = "parallelTest/Main";
    private static final String FUNCT_DEP_ATT = "FuncDependies";
    private static final String FUNCT_IDS = "FuncIdRec";
    private FactoryFacade factory =
            FactoryPlaceholder.getInstanceOf().getFactory();

    public void execute() {
        if (true) return;
        ReadFromFile rf = new ReadFromFile(FILE_NAME);
        String idRec = rf.getFuncIdRecortAtt();
        //System.out.println(idRec);
        String funDep = rf.getFuncDepAttr();
        //System.out.println(funDep);
        ClassFileController cfContr = ClassFileController.getInstanceOf();
        ClassFile cFile = cfContr.getClassByFullyQualifiedName(MAIN_CLASS_NAME);
        addAttribute(FUNCT_IDS, idRec, cFile);
        addAttribute(FUNCT_DEP_ATT, funDep, cFile);

    }

    private void addAttribute(String attName, String info, ClassFile cFile) {
        try {
            ConstantPool cp = (ConstantPool) cFile.getConstantPool();
            Util util = new Util();
            int cpIndex = util.checkAndAddUTF8(attName, cp);
            if (cp.getMaxSize() < cpIndex) {
                cp.setMaxSizeSpecial(cpIndex);
            }
            Un attributeNameIndex = factory.createUn(cpIndex).trim(2);
            Un attributeLength = factory.createUn(info.length()).trim(4);
            byte[] infoBytes = info.getBytes("UTF8");
            Un infoUn = factory.createUn(infoBytes);
            GenericAtt genAtt = factory.createGenericAttribute(attributeNameIndex, attributeLength);
            genAtt.setInfo(infoUn);
            AttributeInfoController attrInfoContr = cFile.getAttributeInfoController();
            int currentAttrCount = attrInfoContr.getCurrentSize();
            attrInfoContr.setMaxSize(currentAttrCount + 1);
            cFile.addAttributeInfo(genAtt);
        } catch (Exception d) {
            d.printStackTrace();
            System.exit(1);
        }
    }
}
